import router from './router/index'
import {
    store
} from './store'
import Cookies from "js-cookie"
import axios from "axios";
//检验token是否存在
function getToken() {
    var token = "";
    token = store.getters.token;
    if (token) {
        return token;
    }
    token = Cookies.get('token');
    console.log(token)
    if (token) {
        return token;
    } else {
        return "";
    }
}

function getCourseTypeList(){
    axios.get("/api/gym/course/getCourseList")
      .then(res => {
        if(res.data.status === 200){
        let data = res.data.data;
        console.log(data)
        store.commit('setCourseType',data)
        }
      })
}

function getCoachList(){
    axios.get("/api/gym/coach/getCoaChList")
    .then(res => {
        if(res.data.status === 200){
            let data = res.data.data;
            store.commit('setCoachList',data)
        }
    })
}

router.beforeEach((to, from, next) => {
    if (to.path != '/login') {
        var token = getToken();
        console.log(token)
        if (!token) { //如果尚未登录，跳转到登录界面
            return next('/login');
        } else {
            getCourseTypeList();
            getCoachList()
        }
    }
    next();
});